package com.hp.easyBuy.controller;

import com.hp.easyBuy.contants.StatusCode;
import com.hp.easyBuy.entity.TUser;
import com.hp.easyBuy.service.UserService;
import com.hp.easyBuy.utils.R;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/*
* 用户类的控制器，负责接收前端的请求并响应结果给前端
* 作者:代奥
* 时间：2024年3月31日 14:02:15
* */
@RestController
@RequestMapping("user")
public class UserController {
    @Resource
    private UserService userService;

    //修改用户密码
    @PostMapping("updatePwd")
    public R updatPwd(String userId,String passWord,String newPwd){
        boolean b = userService.checkUser(userId, passWord);
        if(b){
            boolean result = userService.updatePwd(userId, newPwd);
            if(result){
                return R.success(StatusCode.SUCCESS,"用户密码修改成功");
            }else {
                return R.error(StatusCode.ERROR4,"用户密码修改失败");
            }
        }else {
            return R.error(StatusCode.ERROR3,"输入的原密码错误");
        }
    }

    //修改用户资料
    @RequestMapping("updateUser")
    public R updateUser(@RequestBody TUser user){
        boolean b = userService.updateById(user);
        if(b){
            return R.success();
        }else {
            return R.error();
        }
    }
}
